export default class VNode {
    _rexarr;
    _parent;
    _attrs = {};
    _children = [];

    constructor(rexarr) {
        this._rexarr = rexarr;
    }

    set parent(parent) {
        this._parent = parent;
    }

    get parent() {
        return this._parent;
    }

    getString() {
        return this._rexarr[0];
    }

    getName() {
        return this._rexarr[1];
    }

    addAttr(k, v) {
        this._attrs[k] = v;
    }

    addChild(child) {
        this._children.push(child);
        child.parent = child;
    }

    stringifyAttrs() {
        return Object.keys(this._attrs).map((k)=>{
            if (k === '@click') {
                return `"${k}": ctx.${this._attrs[k]}`
            } else {
                return `"${k}": "${this._attrs[k]}"`
            }
        }).join(",")
    }

    toString() {
        return `
new Component( "${this.getName()}", [
    ${this._children.map((child) => child.toString()).join(",\n\t\t")}], {${this.stringifyAttrs()}})`
    }
}

export class TextNode extends VNode {
    _text = '';

    constructor(rexarr, text) {
        super(rexarr);
        this._text = text;
    }

    toString() {

        return `new Component("span",[\`${this._text.trim()}\`])`
    }
}